<!DOCTYPE html>
<html>
    <?php
    require_once "../DB/db.php";
    include_once '../class/UserClass.php';
    include_once '../Functions/cookieFunctions.php';
    ?>
    <head>
        <title>Nettbutikk</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="../CSS/mainFrame.css" />
        <link rel="stylesheet" type="text/css" href="../CSS/content.css" />
    </head>
    <body>
        <div class="wrapper">
            <?php
            include_once "../MainFrame/header.php";
            include_once "../MainFrame/menuBar.php";
            include_once "../MainFrame/sideBar.php";
            ?>

            <div class="content">
                <?php
                if ($user->isLoggedOn()) {
                    if (isset($_COOKIE['vare'])) {
                        if (!isset($_REQUEST['oid'])) {
                            echo '<h1>Ordeoversikt: </h1>';
                            echo '<table id="kasseTable" style="text-align:left;" >';
                            echo '<tr>';
                            echo '<th width="100px"> Produktid: </th>';
                            echo '<th width="180px"> Produktnavn: </th>';
                            echo '<th width="120px"> Antall: </th>';
                            echo '<th width="120px"> Pris: </th>';
                            echo '<th width="120px"> Totalt: </th>';
                            echo '</tr>';

                            $arr = returnValues('vare');
                            $totPris = 0;
                            for ($i = 0; $i < sizeof($arr); $i +=2) {

                                $id = $arr[$i];
                                $antall = $arr[$i + 1];

                                $items = $db->query("
                                SELECT *
                                FROM item
                                WHERE vid = " . $id
                                );

                                $row = $items->fetch();

                                if ($row) {
                                    $item_name = $row['name'];
                                    $item_vareid = $row['vid'];
                                    $item_price = $row['price'];

                                }

                                /* --------Hver enkelt vare som listes opp i handlekurven:----------- */
                                echo '<tr>';
                                echo '<td>' . $item_vareid . '</td>';
                                echo '<td>' . $item_name . '</td>';
                                echo '<td>' . $antall . '</td>';
                                echo '<td>' . $item_price . ',-</td>';
                                echo '<td>' . $item_price * $antall . ',-</td>';
                                $totPris += ( $item_price * $antall); //Totalprisen i handlekurven
                                echo '</tr>';
                            }
                            echo '<tr>';
                            echo '<td style="text-align:right" colspan="5"> Total pris: ' . $totPris . ',-</td>';
                            echo '</tr>';
                            echo '</table>';

                            echo '<form id="oppdater" method="POST" action="/Functions/kassaFunctions.php">';
                            echo '<h3>Betalingsmetode:</h3>';
                            echo '<input type="radio" name="betMaate" value="faktura" /> Faktura <br />';
                            echo '<input type="radio" name="betMaate" value="oppkrav" /> Postoppkrav <br />';

                            echo '<h3>Leveringmåte:</h3>';
                            echo '<input type="radio" name="levMaate" value="myPack"/> MyPack  Kr 120,- <br />';
                            echo '<input type="radio" name="levMaate" value="posten"/> Posten  Kr 130,- <br />';
                            echo '<input type="radio" name="levMaate" value="local"/> Hent hos oss Kr 20,- <br />';

                            echo '<h3>Leveres til:</h3>';
                            $userArr = $user->getAllUserData($user->getUid());

                            echo '<p>' .$userArr['givenname'] . ' ' . $userArr['surename'] . '</p>';
                            echo '<p>' .$userArr['address'] . '</p>';
                            echo '<p>' .$userArr['postnr'] . ' ' . $userArr['poststed'] . '</p>';

                            $res4 = $db->query('SELECT * from countries WHERE cid=' . $userArr['country']);
                            $row4 = $res4->fetch();
                            if ($row4) {   // displays the country
                                echo '<p>' .$row4['country'] . '</p>';
                            }
                            echo '<input type="submit" name="sendBestilling" value="Send bestilling"/>';
                            echo '</form>';
                        } else {
                            deleteCookie("vare");
                            echo '<h1>Takk for din bestilling!</h1>';
                            echo '<p style="color:white">Din orde-ID er ' . $_REQUEST['oid'] . '</p>';
                            echo '<a href="/Views/mySite.php">Til min side</a>';
                        }
                    } else {
                        echo '<h2>Du Har ingen varer i handlekurven!</h2>';
                    }
                } else {
                    header("Location: registrer.php");
                }
                ?>
            </div>

            <?php
                include_once "../MainFrame/footer.php";
            ?>
        </div>
    </body>
</html>